﻿#include <QTcpSocket>
#include "VirtualDevice.h"

VirtualDevice::VirtualDevice(QObject *parent)
    : Device{parent}
    , m_pDescription((Description*)parent)
{

}

VirtualDevice::~VirtualDevice(){
    uninitDevice();
}

int VirtualDevice::initDevice() {
    connect(this,&VirtualDevice::SendCmdToVirtualDevice,m_pDescription,&Description::ReceviceCmdFromVirtualDevice);
    return 0;
}

void VirtualDevice::uninitDevice() {
    disconnect(this,&VirtualDevice::SendCmdToVirtualDevice,m_pDescription,&Description::ReceviceCmdFromVirtualDevice);
}

QString VirtualDevice::getClassName()
{
    return this->metaObject()->className();
}

void VirtualDevice::writeXML(QXmlStreamWriter &writer)
{
    // 写入当前操作的信息
    writer.writeStartElement("VirtualDevice");
    writer.writeAttribute(QStringLiteral("id"), m_strID);

    writer.writeEndElement(); // 结束当前操作的元素
}

void VirtualDevice::readFromXML(QXmlStreamReader &reader)
{
    QXmlStreamAttributes attributes = reader.attributes();
    if (attributes.hasAttribute("id")) {
        m_strID = attributes.value("id").toString();
    }

    QXmlStreamReader::TokenType token = reader.readNext();
    while (!(reader.tokenType() == QXmlStreamReader::EndElement
             && reader.name() == QStringLiteral("VirtualDevice"))) {
        reader.readNext();
    }
}

void VirtualDevice::writeData(const QVariant &data) {
    emit SendCmdToVirtualDevice(data);
}
